#!/bin/bash

LIB_PATH="../lib"

function get_hpima()
{
	rm -rf ~/hpima*

	#Check functions
	if [ ! -f $LIB_PATH/scp.exp -o ! -f $LIB_PATH/scr.exp ] ; then
		echo "Can not go without scp/scr functions, exit!"
		exit 1
	fi
	
	#Get the latest hpima
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "svn up ./hpima/ezbmc"
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "svn up ./hpima/ezpci"
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "svn up ./hpima/hpmgmtbase"
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "svn up ./hpima"
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "tar -czf ./hpima.tar.gz ./hpima"
	
	$LIB_PATH/scp.exp iforgot dahai@linuxqa.chn.hp.com:/stuff/dahai/hpima.tar.gz ~
	$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "rm -rf ./hpima.tar.gz"
	
	tar -xzf ~/hpima.tar.gz -C ~
	return 0
}

function get_suppcd()
{
	rm -rf ~/SupportPack*

	#Get the latest support cd
	RELEASE=`rpm -qf /etc/redhat-release 2> /dev/null | sed 's/[A-Z].*//' | awk -F '-' '{ printf("rhel%s", $NF); }' `
	if [ "$RELEASE" == "" ] ; then
		RELEASE=`rpm -qf /etc/SuSE-release 2> /dev/null | sed 's/-[0-9]*\.[0-9]*//g' | awk -F '-' '{ printf("sles%s", $NF); }' `
	fi
	if [ "$RELEASE" == "" ] ; then
		echo "Not supported Linux version, exit!"
		exit -1
	fi
	export RELEASE
	
	SPTAR_FULL_PATH=`$LIB_PATH/scr.exp iforgot dahai@linuxqa.chn.hp.com /stuff/dahai "find /pbfd/manageability/ -name SupportPack* | grep -v md5sum | grep -v .iso | grep $RELEASE | sort | tail -n 1" | grep hpiefpl | grep -v "grep" | awk '{ gsub(/\r|\n/, "" ); print $0;}' `
	
	SPTAR_NAME=`echo $SPTAR_FULL_PATH | awk -F '/' '{print $NF;}'`
	$LIB_PATH/scp.exp iforgot dahai@linuxqa.chn.hp.com:$SPTAR_FULL_PATH ~
	
	tar -xf ~/$SPTAR_NAME -C ~
	return 0
}

get_hpima

get_suppcd



